package com.flight.trans.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.activity.LeavePortDuplicateDetailsActivity;
import com.flight.trans.activity.LeavePortHandoverActivity;
import com.flight.trans.adapter.LeavePortDuplicateAdapter;
import com.flight.trans.bean.LeavePortDuplicateBean.LeavePortDuplicateData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.util.ChinaToPinyin;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ProgressUtils;
import com.flight.trans.view.HeaderTitleView;
import com.flight.trans.base.BaseFragment;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 出港复重
 */
public class LeavePortDuplicateFragment extends BaseFragment {

    private HeaderTitleView mHeaderView ;

    private TwinklingRefreshLayout mRefreshLayout ;
    private RecyclerView mRvContainer ;
    private RelativeLayout mRlContentNull ;

    private TextView mTvFlightNumber,//航班号
            mTvAta ,//计起时间
            mTvRouteLine ,//航线
            mTvDispenseCount ,//预配件数
            mTvDispenseWeight ,//预配重量
            mTvDispensePerson ,//预配人
            mTvContainerCount ,//斗数
            mTvGroupBoardPerson ;//组板人

    private LeavePortDuplicateAdapter mDuplicateAdapter;
    private List<LeavePortDuplicateData> mDuplicateDataList ,mSearchDataList;

    private int mSortState = -1;

    @Override
    public int setContentView() {
        return R.layout.fragment_leave_port_duplicate;
    }

    @Override
    public void initView() {
        mTvFlightNumber = mView.findViewById(R.id.mTvFlightNumber);

        mHeaderView = mView.findViewById(R.id.mHeaderView);
        mRefreshLayout = mView.findViewById(R.id.mRefreshLayout);
        mRvContainer = mView.findViewById(R.id.mRvContainer);
        mRlContentNull = mView.findViewById(R.id.mRlContentNull);


        mTvFlightNumber = mView.findViewById(R.id.mTvFlightNumber);
        mTvAta = mView.findViewById(R.id.mTvAta);
        mTvRouteLine = mView.findViewById(R.id.mTvRouteLine);
        mTvDispenseCount = mView.findViewById(R.id.mTvDispenseCount);
        mTvDispenseWeight = mView.findViewById(R.id.mTvDispenseWeight);
        mTvDispensePerson = mView.findViewById(R.id.mTvDispensePerson);
        mTvContainerCount = mView.findViewById(R.id.mTvContainerCount);
        mTvGroupBoardPerson = mView.findViewById(R.id.mTvGroupBoardPerson);
    }

    @Override
    public void initData() {
        initHeaderView();

        mTvFlightNumber.setOnClickListener(this::onClickSort);
        mTvAta.setOnClickListener(this::onClickSort);
        mTvRouteLine.setOnClickListener(this::onClickSort);
        mTvDispenseCount.setOnClickListener(this::onClickSort);
        mTvDispenseWeight.setOnClickListener(this::onClickSort);
        mTvDispensePerson.setOnClickListener(this::onClickSort);
        mTvContainerCount.setOnClickListener(this::onClickSort);
        mTvGroupBoardPerson.setOnClickListener(this::onClickSort);

        CommonUtils.setScreenViewVisible(mTvDispenseCount);
        CommonUtils.setScreenViewVisible(mTvDispenseWeight);
        CommonUtils.setScreenViewVisible(mTvContainerCount);

        setRefreshListener(mRefreshLayout);
    }

    @Override
    public void initConnect() {
        if(mHeaderView == null) return;

        valueParams = new HttpParams();
        valueParams.put(Constant.Key_Done , mHeaderView.isDone());
        if(mHeaderView.isDone())
            valueParams.put(Constant.Key_Flight_Date , mHeaderView.getSelectDate());

        NetworkUtils.getLeavePortDuplicateList(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mDuplicateDataList = (List<LeavePortDuplicateData>) body;

                onScanResult(mHeaderView.getSearchContent());
            }
        }, valueParams);
    }

    private void initAdapter(){
        mRlContentNull.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.GONE : View.VISIBLE);

        if(mDuplicateAdapter == null) {
            mDuplicateAdapter = new LeavePortDuplicateAdapter(mActivity, mSearchDataList);
            mRvContainer.setAdapter(mDuplicateAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mDuplicateAdapter.setOnItemClickListener(new DuplicateOnItemClickListener());
        }else {
            mDuplicateAdapter.updateAdapter(mSearchDataList);
        }
    }

    /**
     * 条目点击事件
     */
    private class DuplicateOnItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            NetworkUtils.checkPageLock(new OkGoBackListener(mActivity) {
                @Override
                public void onSuccess(Object body) {
                    mIntent = new Intent(mActivity , LeavePortDuplicateDetailsActivity.class);
                    mBundle = new Bundle();
                    mBundle.putSerializable(Constant.INFO , mSearchDataList.get(position));
                    mBundle.putBoolean(Constant.IsEditor , !mHeaderView.isDone());
                    mIntent.putExtras(mBundle);
                    turnActForResult(mIntent);
                }
            }, ConstantInfo.OUT_STORAGE_OUT , mSearchDataList.get(position).getFlightId());
        }

        @Override
        public void onLongClick(View view, int position) {
            if(mHeaderView.isDone()){
                mIntent = new Intent(mActivity , LeavePortHandoverActivity.class);
                mBundle = new Bundle();
                mBundle.putBoolean(Constant.IsEditor , mHeaderView.isDone());
                mBundle.putInt(Constant.FlightID , mSearchDataList.get(position).getFlightId());
                mIntent.putExtras(mBundle);
                turnActForResult(mIntent);
            }
        }
    }

    @Override
    public void onScanResult(String code) {
        mHeaderView.setSearchContent(code);

        mSearchDataList = new ArrayList<>();

        if(!TextUtils.isEmpty(code)) {
            for (int i = 0; i < mDuplicateDataList.size(); i++)
                if(mDuplicateDataList.get(i).getFlightNo() != null && mDuplicateDataList.get(i).getFlightNo().contains(code))
                    mSearchDataList.add(mDuplicateDataList.get(i));
        }else {
            mSearchDataList.addAll(mDuplicateDataList) ;
        }

        if(mSearchDataList != null)
            if(mSortState == 0){
                Collections.reverse(mSearchDataList);
            }else if(mSortState != -1){
                Collections.sort(mSearchDataList, new SortComparator(mSortState));
            }

        initAdapter();
    }

    private void initHeaderView() {
        mHeaderView.addOnHeaderListener(new HeaderTitleView.OnHeaderListener(){
            @Override
            public void onSearch(String search) {
                onScanResult(search);
            }

            @Override
            public void onPartChange(int isChecked) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }

            @Override
            public void onDateChange(String date) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }
        });
    }


    public void onClickSort(View view){
        mSortState = (mSortState != 0 && R.id.mTvFlightNumber == view.getId()) ? 0 : (mSortState == 0 ? -1 : mSortState) ;
        mSortState = (mSortState != 1 && R.id.mTvAta == view.getId()) ? 1 : mSortState == 1 ? -1 : mSortState ;
        mSortState = (mSortState != 2 && R.id.mTvRouteLine == view.getId()) ? 2 : mSortState == 2 ? -1 : mSortState ;
        mSortState = (mSortState != 3 && R.id.mTvDispenseCount == view.getId()) ? 3 : mSortState == 3 ? -1 : mSortState ;
        mSortState = (mSortState != 4 && R.id.mTvDispenseWeight == view.getId()) ? 4 : mSortState == 4 ? -1 : mSortState ;
        mSortState = (mSortState != 5 && R.id.mTvDispensePerson == view.getId()) ? 5 : mSortState == 5 ? -1 : mSortState ;
        mSortState = (mSortState != 6 && R.id.mTvContainerCount == view.getId()) ? 6 : mSortState == 6 ? -1 : mSortState ;
        mSortState = (mSortState != 7 && R.id.mTvGroupBoardPerson == view.getId()) ? 7 : mSortState == 7 ? -1 : mSortState ;

        mTvFlightNumber.setTextColor(mActivity.getColor(mSortState == 0 ? R.color.color_red : R.color.color_blue));
        mTvAta.setTextColor(mActivity.getColor(mSortState == 1 ? R.color.color_red : R.color.color_blue));
        mTvRouteLine.setTextColor(mActivity.getColor(mSortState == 2 ? R.color.color_red : R.color.color_blue));
        mTvDispenseCount.setTextColor(mActivity.getColor(mSortState == 3 ? R.color.color_red : R.color.color_blue));
        mTvDispenseWeight.setTextColor(mActivity.getColor(mSortState == 4 ? R.color.color_red : R.color.color_blue));
        mTvDispensePerson.setTextColor(mActivity.getColor(mSortState == 5 ? R.color.color_red : R.color.color_blue));
        mTvContainerCount.setTextColor(mActivity.getColor(mSortState == 6 ? R.color.color_red : R.color.color_blue));
        mTvGroupBoardPerson.setTextColor(mActivity.getColor(mSortState == 7 ? R.color.color_red : R.color.color_blue));

        onScanResult(mHeaderView.getSearchContent());
    }

    /**
     * 排序集合
     */
    private class SortComparator implements Comparator<LeavePortDuplicateData> {

        private int type;

        public SortComparator(int type){
            this.type = type;
        }

        @Override
        public int compare(LeavePortDuplicateData data1, LeavePortDuplicateData data2) {
            switch (type){
                case 1 :
                    // 计起时间
                    return (int) (CommonUtils.getTimeLong(data2.getStd()) - CommonUtils.getTimeLong(data1.getStd()));
                case 2 :
                    return ChinaToPinyin.getInstance().sort(data1.getFlightCourseCode() , data2.getFlightCourseCode());
                case 3 :
                    return data2.getDispenseNum() - data1.getDispenseNum();
                case 4 :
                    return (int) (data2.getDispenseWeight() - data1.getDispenseWeight());
                case 5 :
                    return ChinaToPinyin.getInstance().sort(data1.getDispenseUserName() , data2.getDispenseUserName());
                case 6 :
                    return data2.getOutboundCnt() - data1.getOutboundCnt();
                case 7 :
                    return ChinaToPinyin.getInstance().sort(data1.getAssembleUserName() , data2.getAssembleUserName());
                default:
                    return 0 ;
            }
        }
    }
}
